####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Tab A13-A15
###
####################################################################################


library(dplyr)
library(psych)
library(xtable) 


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################ 
#   VARIABLES
################################################################################

df_did$num_empty_log = df_did$num_empty_log/df_did$num_log
df_did$num_res = df_did$num_res/df_did$num_log
df_did$num_room_res = df_did$num_room_res/df_did$num_log
df_did$num_res_own = df_did$num_res_own/df_did$num_log

data$num_empty_log = data$num_empty_log/data$num_log
data$num_res = data$num_res/data$num_log
data$num_room_res = data$num_room_res/data$num_log
data$num_res_own = data$num_res_own/data$num_log

outvars = c('groupT', 'sruT',  'pop', 'imm', 'foreign', 'num_hlm', 'hlm_share', "num_empty_log", "num_res", "num_room_res" ,
            'num_log', 'num_res_own', 'median_inc',  'border_agglo',
            'turnout', 'fn', 'extreme.left', 'left', 'right')

outvars_name = c( 'SRU group', 'SUR', 'Population', 'Immigration', 'Foreigners', 'Number of HLM', 'HLM Share',
                  'Share of Empty dwellings', 'Share of Primary Residences',
                  'Avg Number of Rooms per Dwelling', 
                  'Number of Dwellings', 'Share of Dwelling Owned','Median Income',
                  'Border', 'Turnout', 'National Front', 'Extreme Left', 'Left', 'Right')


################################################################################
# Table A13
################################################################################

temp = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_quant_99) & !is.na(df_did$running) & !is.na(df_did$policy_period) & !is.na(df_did$CODGEO)),]

container = data.frame()

for (j in 1:length(outvars)){
  var = outvars[j]
  print(var)
  sum = unname(describe(temp[var])[c(2, 3, 4, 5, 8, 9)])
  container = rbind(container, c(outvars_name[j], sum))
}

colnames(container) = c('', 'n', 'mean', 'sd', 'median', 'min', 'max')
print(xtable(container, digits = c(0,0,0 ,2,2,2,2,2)) ,include.rownames =FALSE)



################################################################################
# Table A14
################################################################################

temp = data[which(!is.na(data$agglo_name)),]

container = data.frame()

for (j in 1:length(outvars)){
  var = outvars[j]
  sum = unname(describe(temp[var])[c(2, 3, 4, 5, 8, 9)])
  container = rbind(container, c(outvars_name[j], sum))
}

colnames(container) = c('', 'n', 'mean', 'sd', 'median', 'min', 'max')
print(xtable(container, digits = c(0,0,0 ,2,2,2,2,2)), include.rownames =FALSE)


################################################################################
# Table A15
################################################################################

temp = data

container = data.frame()

for (j in 1:length(outvars)){
  var = outvars[j]
  sum = unname(describe(temp[var])[c(2, 3, 4, 5, 8, 9)])
  container = rbind(container, c(outvars_name[j], sum))
}

colnames(container) = c('', 'n', 'mean', 'sd', 'median', 'min', 'max')
print(xtable(container, digits = c(0,0,0 ,2,2,2,2,2)), include.rownames =FALSE)


